package com.smartao.base.entityitem;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.smartao.base.module.Module;
import com.smartao.core.mvc.BaseAction;
import com.smartao.core.orm.Page;
import com.smartao.core.orm.PageRequest;

@Controller
@Scope("prototype")
@Results({ 
	@Result(name = BaseAction.RELOAD, type="redirect", location="entityitem.action",params={"moduleId","${moduleId}"}),
	@Result(name = BaseAction.SUCCESS, location="entityitem.jsp"),
@Result(name = BaseAction.INPUT, location="entityitem-input.jsp")})
public class EntityitemAction extends BaseAction {

	private static final long serialVersionUID = -5431131738114276797L;
	
	@Autowired
	private EntityitemService entityitemService;
	
	private Entityitem entityitem;
	private Page<Entityitem> page;
	
	private Long moduleId;
	
	@Override
	public String execute(){
		PageRequest pageRequest = new PageRequest(pageNumber,pageSize);
		page = entityitemService.pageQuery(entityitem,pageRequest);
		return SUCCESS;
	}
	
	@Override
	public String input(){
		if(id!=null){
			entityitem = entityitemService.findOne(id);
		}else{
			entityitem = new Entityitem();
			entityitem.setModule(new Module(moduleId));
		}
		return INPUT;
	}
	
	public String save(){
		entityitemService.save(entityitem);
		//重新设置RELOAD页面的参数
		moduleId = entityitem.getModule().getId();
		return RELOAD;
	}

	
	public Entityitem getEntityitem() {
		return entityitem;
	}

	public void setEntityitem(Entityitem entityitem) {
		this.entityitem = entityitem;
	}

	public Page<Entityitem> getPage() {
		return page;
	}

	public void setModuleId(Long moduleId) {
		this.moduleId = moduleId;
	}

	public Long getModuleId() {
		return moduleId;
	}
}
